<?php
class model_payout extends common_model {
	/**
	 * 基本表属性
	 * @var string
	 */
	protected $table = 'payout';
	protected $pk = 'pay_id';
	
	/**
	 * 获取时间范围
	 * @return array
	 */
	public function get_range($where = '1') {
		$res = $this->find($where, '', 1, 'MAX(create_time) max, MIN(create_time) min');
		//today
		$today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
		
		$res['max'] < $today && $res['max'] = $today;
		$res['min'] == '' && $res['min'] = $today;
		$res['count'] = ($res['max'] - $res['min']) / 86400 + 1;
		return $res;
	}
	
	/**
	 * 计算总和
	 * @param int $amount 数额限制
	 * @return int
	 */
	public function get_sum($amount_limit = 0) {
		$where = $amount_limit == 0 ? '1' : 'amount<'.$amount_limit;
		$res = $this->find($where, '', 1, 'SUM(amount) sum');
		return $res['sum'];
	}
	
	/**
	 * 计算近31天的和 (包含今天与之前30天)
	 * @param int $amount 数额限制
	 * @return int
	 */
	public function get_sum_30($amount_limit = 0) {
		$where = $amount_limit == 0 ? '1' : 'amount<'.$amount_limit;
		$time_start = time() - 86400*30;
		$res = $this->find("create_time > '$time_start' AND $where", '', 1, 'SUM(amount) sum');
		return $res['sum'];
	}
	
	/**
	 * 根据时间获取总和
	 * @param int $time_start 开始时间
	 * @return int
	 */
	public function get_sum_by_time($time_start) {
		$where = 'create_time>='.$time_start;
		$res = $this->find($where, '', 1, 'SUM(amount) sum');
		return $res['sum'];
	}
	
	/**
	 * 获取每天的数据
	 * @return array
	 */
	public function get_daily_data($where) {
		//原始数据
		$res = $this->find_all($where);
		//时间范围
		$pay_range = $this->get_range($where);
		//每天数据
		$daily_data = array();

		//初始每天数据 改为倒序->改为正序
		for($i = $pay_range['min']; $i <= $pay_range['max']; $i += 86400) {
			$daily_data[$i]['info']['sum'] = 0;
			$daily_data[$i]['info']['time'] = $i;
			$daily_data[$i]['data'] = array();
		}
		//赋值每天数据
		foreach($res as $row) {
			$daily_data[$row['create_time']]['data'][] = $row;
			$daily_data[$row['create_time']]['info']['sum'] += $row['amount'];
		}

		//返回
		return $daily_data;
	}
	
	/**
	 * 获取每天的统计数据
	 */
	public function get_daily_stat($where = 1) {
		// 每天数据
		$res = $this->find_all($where.' GROUP BY create_time', '', '', 'SUM(amount) sum, create_time');
		$pay_range = $this->get_range();
		$data = array();
		foreach($res as $row) {
			$data[$row['create_time']]['sum'] = $row['sum'];
		}

		// 整理补全数据
		$daily_stat = array();
		$temp_avg_pre = 0;
		for($i = $pay_range['min']; $i <= $pay_range['max']; $i += 86400) {
			// 当天的总额
			$daily_stat[$i]['sum'] = isset($data[$i]['sum']) ? $data[$i]['sum'] : '0';
			// 累计的总额
			$daily_stat[$i]['sum_all'] = isset($daily_stat[$i - 86400]['sum_all']) ? $daily_stat[$i - 86400]['sum_all'] + $daily_stat[$i]['sum'] : $daily_stat[$i]['sum'];
			// 已统计的天数
			$daily_stat[$i]['day_count'] = ($i - $pay_range['min']) / 86400 + 1;
			// 平均
			$daily_stat[$i]['avg'] = round($daily_stat[$i]['sum_all'] / $daily_stat[$i]['day_count'], 2);
			// 趋势
			$daily_stat[$i]['avg_trend'] = round($daily_stat[$i]['avg'] - $temp_avg_pre, 2);
			// 前30天的累计总额
			$sum_30_pre = isset($daily_stat[$i - 86400]['sum_30']) ? $daily_stat[$i - 86400]['sum_30'] : '0';
			$sum_pre30 = isset($daily_stat[$i - 86400*30]['sum']) ? $daily_stat[$i - 86400*30]['sum'] : '0';
			$daily_stat[$i]['sum_30'] = $sum_30_pre - $sum_pre30 + $daily_stat[$i]['sum'];
			// 前30天的累计日均
			$day_count = $daily_stat[$i]['day_count'] < 30 ? $daily_stat[$i]['day_count'] : 30;
			$daily_stat[$i]['avg_30'] = round($daily_stat[$i]['sum_30'] / $day_count, 2);
			
			$temp_avg_pre = $daily_stat[$i]['avg'];
		}
		
		return $daily_stat;
	}
	
	/**
	 * 添加日志
	 * @param string $body 日志内容
	 */
	public function log_write($body) {
		$sum_now = $this->get_sum();
		new model_logs('['.$sum_now.'] - '.$body);
	}
}
?>